package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO 购物车数据传输模型
     */
    @Transactional
    public void save(ShoppingCartDTO shoppingCartDTO) {
        // 判断当前加入到购物车的数据是否实现
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setCreateTime(LocalDateTime.now());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        // 如果存在，则数量加一
        if (shoppingCartList != null && shoppingCartList.size() > 0){
            shoppingCart = shoppingCartList.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.update(shoppingCart);
        } else {
            // 如果不存在，则插入数据
            Long dishId = shoppingCartDTO.getDishId();
            if (dishId != null){
                // 本次插入的是菜品数据
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 本次插入的是套餐数据
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.Insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return ShoppingCart集合
     */
    public List<ShoppingCart> ShowShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    public void CleanShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.deleteByUserId(shoppingCart);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO 购物车数据传输模型
     */
    public void SubShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 封装查询条件购物车实体类
        ShoppingCart shoppingCart = new ShoppingCart();
        // 将购物车数据传输模型的值复制给购物车实体类
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 完善购物车实体类中的用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 根据封装好的购物车实体类查询购物车数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        // 判断购物车中是否有该商品
        if (shoppingCartList != null && shoppingCartList.size() > 0){
            // 获取购物车中该商品
            shoppingCart = shoppingCartList.get(0);
            // 获取购物车中该商品的份数
            Integer number = shoppingCart.getNumber();
            if (number > 1) {
                // 大于1，修改份数
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.update(shoppingCart);
            }else {
                // 小于/等于 1，删除该条数据
                shoppingCartMapper.deleteByUserId(shoppingCart);
            }
        }
    }
}
